import axios from 'axios'
import { ElMessage } from 'element-plus';
import { lo } from 'element-plus/es/locales.mjs';

const http =axios.create({
  baseURL: 'https://v3pz.itndedu.com/v3pz',
  timeout: 10000
})

http.interceptors.request.use(config => {
  // Do something before request is sent

  const token = localStorage.getItem('pz_token')

  const withUrl =['/get/code','/user/authentication','/login']

  if(token && !withUrl.includes(config.url)){
    config.headers['x-token'] = token
  }
  return config;

  return config;
}, function (error)  {
  // Do something with request error
  return Promise.reject(error);
});

http.interceptors.response.use(function (response) {
  // Do something with response data
  if(response.data.code === -1){
   ElMessage.warning(response.data.message)
 }
 if(response.data.code === -2){
    localStorage.removeItem('pz_token')
    localStorage.removeItem('pz_userInfo')
    localStorage.removeItem('pz_v3pz')
    window.location.href = window.location.origin
 }
  return response;
}, function(error)  {
  // Do something with response error
  return Promise.reject(error);
});

export default http;